<?php

namespace app\controller;

use app\service\ProductService;
use app\controller\BaseController;
use think\Request;
use kdy\rabbitmq\Rabbitmq;
use think\facade\Log;

class ProductController extends BaseController
{

    /**
     * 根据关键词查询
     * @param string $keywords
     * @return json
     */
    public function search(Request $request)
    {
        $keyword = $request->param('keyword','');

        $productService = ProductService::instance();

        //搜索商品
        $res = $productService->searchProduct($keyword);
        return $this->renderJson($res);
    }

    public function update(Request $request)
    {
        $goodsId = $request->param('goodsId',0);
        $source = $request->param('source',0);
        if(!$goodsId || !$source){
            return false;
        }
        ProductService::instance()->updateProduct($goodsId,$source);
        return $this->renderJson();
    }

    //发送消息demo
    public function push()
    {
        $mq = Rabbitmq::instance();
        $job = $mq->onConnection('kdy')
                ->onExchange('shop')
                ->onQueue('update_product');
        for($i=1;$i<=100;$i++){
            $msg = ['goods_id'=>$i,'data_source'=>1];
            echo json_encode($msg)."\n";
            $job->dispatch($msg);
        }
    }

}
